package cn.ym.mall.dao.impl;

import cn.ym.mall.dao.RoleDao;
import cn.ym.mall.entity.Role;
import cn.ym.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class RoleDaoImpl implements RoleDao {
    QueryRunner queryRunner=new QueryRunner();
    /**
     * 获得所有角色信息
     *
     * @return
     */
    @Override
    public List<Role> getAll() throws SQLException{
        String sql="select rid, rname from ym_role;";
        List<Role> roleList=null;
        try{
            roleList= queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<Role>(Role.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return roleList;
    }

    /**
     * 添加新角色信息
     *
     * @param role
     * @return
     */
    @Override
    public int add(Role role) throws SQLException{
        String sql="insert into ym_role(rid,rname) value(?,?);";
        Object[]  arr={role.getRname()};
        int num=0;
        try{
            num= queryRunner.update(DruidUtils.getConnection(),sql,arr);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 根据角色权限Id删除角色信息
     *
     * @param rid
     * @return
     */
    @Override
    public int delete(Integer rid) throws SQLException {
        String sql="delete from ym_role where rid=?;";
        Object[]  arr={rid};
        int num=0;
        try{
            num= queryRunner.update(DruidUtils.getConnection(),sql,arr);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 更新角色信息
     * @param role
     * @return
     */
    @Override
    public int update(Role role) throws SQLException{
        String sql="update ym_role set rname=? where rid=?;";
        Object[]  arr={role.getRname(),role.getRid()};
        int num=0;
        try{
            num= queryRunner.update(DruidUtils.getConnection(),sql,arr);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 获得角色信息总数
     *
     * @return
     */
    @Override
    public long getCount() throws SQLException{
        long count = 0;
        try{
            String sql = "select count(0) as count from ym_role;";
            count = queryRunner.query(DruidUtils.getConnection(),sql,new ScalarHandler<>());
        }catch (Exception e){
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 根据角色编号获得角色信息
     * @param rid
     * @return
     * @throws SQLException
     */
    @Override
    public Role getRoleByRid(Integer rid) throws SQLException {
        Role Role=null;
        try{
            String sql="select rid, rname from ym_role where rid=?;";
            Role= queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<Role>(Role.class),rid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return Role;
    }


}
